<template>
  <div id="updateDialog">
    <el-dialog class="boxes" title="添加设备" :visible.sync="updateDialogShow" :before-close="handleClose">
     <div class="content">
      <div class="leftTree1">
        <div class="treeSearch">
          <el-input type="text"   placeholder="" class="searchTree" v-model="submitsearch.key"></el-input>
          <i class="el-icon-search searchicon" @click="searchTreeData"></i>
        </div>
        <el-tree
          :data="ztreeDataSource1"
          :props="defaultProps"
          :highlight-current="true"
          @node-click="handleCheckChange1"
          >
        </el-tree>
      </div>
       <div class="rightTable">
         <div class="tableSearch">
           <div class="title">
            设备列表
           </div>
           <div class="rightSearch">
             <el-input  placeholder="" v-model="submitSearchEquip.id"></el-input>
             <i class="el-icon-search search" @click="searchData"> </i>
           </div>
         </div>
         <el-table
           :data="tableData"
           border
           stripe border fit highlight-current-row
           style="width: 100%;height:250px; "
           :header-cell-style="{background:'#F7F8F9',height: '20px',padding:'0px 0px'}"
           @selection-change="moreDelete"
           height="250px"
         >
           <el-table-column type="selection" align="center" width="40" @selection-change="moreDelete"></el-table-column>

           <el-table-column
             type="index"
             label="序号"
             width="60px"
           >
           </el-table-column>
           <el-table-column
             v-for="(column, index) in cols"
             :prop="column.prop"
             :label="column.label"
             :key="index"
             :width="column.width"
             :sortable="column.sortable"
           >
           </el-table-column>
           <el-table-column
             type="index"
             label="数量"
             width="219px"
           >
             <template  slot-scope="scope">
               <el-button class="tableIcon" title="删除"  type="text" size="small" v-if="scope.row.count >=2" @click="deleteOne(scope.$index,scope.row)">
                 <i class="el-icon-minus deleteIcon" ></i>
               </el-button>
               <el-input  placeholder="" v-model="scope.row.count" class="addCount" id="num" value="" ></el-input>
               <el-button  class="tableIcon addIcon" title="增加" type="text" size="small" @click="addOne(scope.$index,scope.row)">
                 <i class="el-icon-plus"></i>
               </el-button>
             </template>
           </el-table-column>
         </el-table>
         <div class="pagination-container footerPage">
           <el-pagination
             @current-change="CurrentChange"
             @size-change="SizeChange"
             :page-sizes="[5, 10, 20, 50]"
             background layout="total, sizes, prev, pager, next"
             :total="total">
           </el-pagination>
         </div>
       </div>

     </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addEquip">保存</el-button>
        <el-button type="info" @click="handleClose" >取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import axios from 'axios'
  import treeBus from './bus.js'
  export default {
    props: {
      propsgid: ''
    },
    components: {
    },
    data() {
      return {
        searchvalue: '',
        restatus: '更新',
        returngid: this.propsgid,
        tableCount: 1,
        tableCount1: '2',
        projectPid: this.$route.query.pid,
        devices: {},
        resouceId: '',
        ztreeDataSource1: [],
        defaultProps: {
          child: 'child',
          label: 'name',
          isLeaf: 'leaf'
        },
        // 以下为接口
        submitdata: {
          access_token: localStorage.getItem('access_token'),
          type: 'projects_type_getall',
          pid: 'ZD201809000002',
          id: '1'
        },
        submitAddEquip: {
          access_token: localStorage.getItem('access_token'),
          type: 'object_batch_add',
          pid: 'ZD201809000002',
          data: {
            gid: '',
            device: []
          }
        },
        submitSearchEquip: {
          access_token: localStorage.getItem('access_token'),
          type: 'projects_device_query',
          id: '',
          page: {
            page_size: '10',
            page_number: '1'
          }
        },
        dataList: [],
        treeDeepCopy: [],
        parentNodeModel: [], // 当前点击节点父亲对象
        nodeModel: null, // 当前点击节点对象
        ztreeDataSourceList: [],
        ztreeDataSourceSync: [],
        pages: [],
        updateDialogFalse: false,
        isIndeterminate: true,
        cols: [
          { prop: 'name', label: '名称', width: '120px', sortable: true },
          { prop: 'catagory', label: '分类', width: '80px', sortable: true },
          { prop: 'brand', label: '品牌', width: '80px', sortable: true },
          { prop: 'resource_id', label: '父路径', width: '100px', sortable: true }
        ],
        tableData: [],
        tableData1: [
        ],
        submitsearch: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'protocol_cata_query',
          key: ''
        },
        total: 0,
        projectdata: {
          access_token: localStorage.getItem('access_token'),
          type: 'projects_device_table_getall',
          id: '',
          page: {
            page_size: '10',
            page_number: '1'
          },
          ztree: []
        }
      }
    },
    created() {
      this.updateDialogShow = true
    },
    mounted() {
      // this.queryData()
      this.searchTreeData()
      this.equipData()
    },
    methods: {
      // 子组件传值
      handleClose() {
        const dialogShow = this.updateDialogFalse // 所需要传的值
        this.$emit('giveFalse', dialogShow) // 传值的参数
      },
      searchTreeData() {
        // 设备树检索
        axios.post('/protocol', this.submitsearch, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.ztreeDataSource1 = []
          this.ztreeDataSource1.push(res.data.datas[0])
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      queryData() {
        // 设备树总览
        this.submitdata.pid = this.projectPid
        axios.post('/protocol', this.submitdata, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.ztreeDataSource1.push(res.data.data[0])
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      findById(data, parentId) {
        var vm = this
        for (var i = 0; i < data.length; i++) {
          if (parentId === data[i].id) {
            vm.dataList.push(data[i])
            vm.findById(vm.ztreeDataSource, data[i].parentId)
            return data[i]
          }

          if (data[i].hasOwnProperty('children')) {
            vm.findById(data[i].children, parentId)
          }
        }
      },
      // 点击节点
      nodeClick(m, parent, trees) {
        this.treeDeepCopy = trees
        this.nodeModel = m // 当前点击节点对象
        this.parentNodeModel = parent // 当前点击节点父亲对象

        // 导航菜单
        this.dataList = []
        this.findById(this.ztreeDataSource, m.parentId)
        this.dataList = this.dataList.reverse()
        this.dataList.push(m)
      },
      handleCheckChange1(data, checked, indeterminate) {
        this.resouceId = data.resource_id
        this.equipData()
      },
      handleNodeClick(data) {
        this.devices = data
      },
      // 获取设备总览
      equipData() {
        this.projectdata.id = this.resouceId
        axios.post('/protocol', this.projectdata, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.tableData = res.data.data
          this.total = parseInt(res.data.page.total)
          this.pages = res.data.page
          for (var i = 0; i < this.tableData.length; i++) {
            var con = this.tableData[i].count
          }
          this.tableCount1 = con
        })
          .catch(function(error) {
            console.log(6666, error)
          })
      },
      // 模糊检索设备
      searchData() {
        axios.post('/protocol', this.submitSearchEquip, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.tableData = res.data.data
          this.total = parseInt(res.data.page.total)
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      // 当前页
      CurrentChange(val) {
        this.projectdata.page.page_number = (val) + ''
        this.equipData()
      },
      // 每页显示多少条
      SizeChange(pageSize) {
        this.projectdata.page.page_size = (pageSize) + ''
        this.equipData()
      },
      // 获取多选项
      moreDelete(val) {
        console.log(val)
        this.multipleSelection = val
        var deletemoreid = []
        for (var i = 0; i < this.multipleSelection.length; i++) {
          deletemoreid.push(this.multipleSelection[i])
        }
        this.devices = deletemoreid
      },
      // 添加设备接口
      addEquip() {
        for (var j = 0; j < this.devices.length; j++) {
          var obj = {}
          obj.resource_id = this.devices[j].resource_id
          obj.count = this.devices[j].count
        }
        this.submitAddEquip.data.device.push(obj)
        this.submitAddEquip.data.gid = this.returngid
        this.submitAddEquip.pid = this.$route.query.pid
        axios.post('/project', this.submitAddEquip, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          if (res.data.errcode === '00000') {
            this.handleClose()
            this.restars()
            // window.location.reload()
            this.$message({
              type: 'success',
              message: '保存成功'
            })
          }
        })
          .catch((error) => {
            this.$message({
              type: 'error',
              message: error.data.errmsg
            })
          })
      },
      // 保存成功返回的状态
      restars() {
        treeBus.$emit('rerule', this.restatus)
      },
      // 点击加1  你的勾选
      addOne(index, row) {
        row.count = parseInt(row.count) + 1
        this.tableCount = row.count
      },
      // 点击减1
      deleteOne(index, row) {
        row.count = parseInt(row.count) - 1
        this.tableCount = row.count
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /*****************第一层弹窗*****************/
  .addCount{
    width: 50%;
  }
  .searchTree{
    width: 90%;
  }
  .deleteIcon{
    font-size: 20px;
    font-weight: bold;
    color: #D5664B;
  }
  .addIcon{
    font-size: 20px;
    font-weight: bold;
    color: #0072F3;
  }
  .testColor{
    padding: 0;
    color: #fff;
  }
  .content{
    width: 1000px;
    height: 300px;
    line-height: 50px;
    text-align: center;

  }
  .leftTree1{
    width: 290px;
    height: 300px;
    overflow: auto;
    background-color: #F0F3F5;
    float: left;
    /*.treeSearch{*/
      /*width: 250px;*/
      /*height: 30px;*/
      /*background-color: #d3dce6;*/
      /*margin-left: 20px;*/
    /*}*/
    .searchicon{
      margin-top: 18px;
      position: absolute;
      margin-left: -21px;
      color: #0072F3;
      cursor: pointer;
    }
  }
  .rightTable{
    width: 700px;
    height: 300px;
    float: right;
    .tableSearch{
     width: 100%;
      height: 50px;
      .title{
        width: 10%;
        height: 50px;
        font-size:14px;
        font-family:MicrosoftYaHei-Bold;
        font-weight:bold;
        color:rgba(50,56,66,1);
        float: left;

      }
      .rightSearch{
        width: 40%;
        height: 50px;
        float: right;
        /*margin-right: 20px;*/
      }
      .search{
        margin-top: 17px;
        position: absolute;
        margin-left: -22px;
        color: #0072F3;
      }
    }
  }
  /deep/ .addEditIcon {
    color: #0072F3;
    font-size: 16px;
    cursor: pointer;
  }
  /deep/ .el-range-editor--small.el-input__inner {
    height: 30px;
    width: 385px;
  }
  /deep/ .addSafeTable > .el-table__header-wrapper{
    height: 0px;
  }
  /*****************第一层弹窗*****************/
  /deep/ .boxes > .el-dialog{
    width: 1048px;
    min-height: 370px;
    max-height: 450px;
    overflow: auto;
    margin-top: 20vh !important;
    border-radius: 4px;
    overflow-x: hidden;
  }
  /*****************第一层弹窗头部*****************/
  /deep/ .boxes > .el-dialog > .el-dialog__header{
    width: 1048px;
    height: 39px;
    line-height: 39px;
    padding: 0;
    text-indent: 20px;
    font-weight: bolder;
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    background-color: #e3e6ee;
  }
  /deep/ .boxes > .el-dialog > .el-dialog__header > .el-dialog__headerbtn{
    top: 12px;
  }
  /deep/ .boxes > .el-dialog > .el-dialog__header > .el-dialog__headerbtn > i{
    font-size: 18px;
    font-weight: bolder;
  }
  /*****************第一层弹窗内容*****************/
  /deep/ .boxes > .el-dialog > .el-dialog__body{
    padding: 0 0 0 0px ;
    height: 320px;
    line-height: 30px;
    margin-bottom: 20px;
  }
  /deep/ .gutter{
    background-color: #d8dee8;
  }
  /deep/ .boxes > .el-dialog > .el-dialog__body > .el-form > .el-form-item{
    margin-bottom: 0;
  }
  /deep/ .boxes > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__label{
    color: #69727E;
    font-weight: normal;
  }
  /*****************第一层弹窗底部*****************/
  /deep/ .boxes > .el-dialog > .el-dialog__footer{
    border-top: 1px solid #e3e6ee;
  }
  /*****************底部按钮*****************/
  /deep/ .dialog-footer{
    width: 100%;
    /*padding-top: 8px;*/
  }
  /deep/ .dialog-footer > .el-button{
    width: 80px;
    height: 30px;
    padding: 0;
  }
  /deep/ .el-input__inner {
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    -webkit-boxes-sizing: border-boxes;
    boxes-sizing: border-boxes;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 30px;
    line-height: 30px;
    outline: 0;
    padding: 0 10px;
    -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    width: 100%;
  }
</style>
